<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        .form-control:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
        }
        .btn-register {
            width: 100%;
            padding: 12px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 10px;
        }
        .btn-register:hover {
            background-color: #218838;
        }
        .btn-back {
            display: inline-block;
            margin-bottom: 20px;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .btn-back:hover {
            background-color: #0056b3;
        }
        .result-container {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .result-container.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .result-container.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .token-display {
            font-weight: bold;
            font-size: 18px;
            word-break: break-all;
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="index.html" class="btn-back">← 返回主页</a>
        <h1>设备注册</h1>
        
        <div class="form-group">
            <label for="devid">设备ID (devid):</label>
            <input type="text" id="devid" class="form-control" placeholder="请输入设备ID">
        </div>
        
        <div class="form-group">
            <label for="devmac">设备MAC地址 (devmac):</label>
            <input type="text" id="devmac" class="form-control" placeholder="请输入设备MAC地址">
        </div>
        
        <button class="btn-register" onclick="generateToken()">获取Token</button>
        
        <div id="resultContainer" class="result-container">
            <div id="resultMessage"></div>
            <div id="tokenDisplay" class="token-display" style="display: none;"></div>
        </div>
    </div>

    <script>
        // 生成Token函数
        function generateToken() {
            // 获取表单数据
            const devid = document.getElementById('devid').value.trim();
            const devmac = document.getElementById('devmac').value.trim();
            
            // 验证输入
            if (!devid || !devmac) {
                showMessage('请填写设备ID和设备MAC地址', 'error');
                return;
            }
            
            // 显示加载状态
            showMessage('正在生成Token...', 'success');
            
            // 发送请求到服务器
            fetch('generate_token.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ devid: devid, devmac: devmac })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    showMessage('Token生成成功！', 'success');
                    showToken(data.data.token);
                } else {
                    showMessage('Token生成失败: ' + data.message, 'error');
                    hideToken();
                }
            })
            .catch(error => {
                showMessage('请求失败: ' + error.message, 'error');
                hideToken();
            });
        }
        
        // 显示消息
        function showMessage(message, type) {
            const resultContainer = document.getElementById('resultContainer');
            const resultMessage = document.getElementById('resultMessage');
            
            resultMessage.textContent = message;
            resultContainer.className = 'result-container ' + type;
            resultContainer.style.display = 'block';
        }
        
        // 显示Token
        function showToken(token) {
            const tokenDisplay = document.getElementById('tokenDisplay');
            tokenDisplay.textContent = token;
            tokenDisplay.style.display = 'block';
        }
        
        // 隐藏Token
        function hideToken() {
            const tokenDisplay = document.getElementById('tokenDisplay');
            tokenDisplay.style.display = 'none';
        }
    </script>
</body>
</html>